package com.bears.god.sup.servlet.operation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.bears.god.sup.model.Bill;
import com.bears.god.sup.model.Dept;
import com.bears.god.sup.model.Income;
import com.bears.god.sup.model.Vendor;
import com.bears.god.sup.service.IBillService;
import com.bears.god.sup.service.IncomeService;
import com.bears.god.sup.service.ServiceDAOFactory;

@WebServlet("/listbill")
public class ListBillServlet extends HttpServlet{

	private static final long serialVersionUID = 7047127414156439892L;

	/**
	 * 
	 */
	public static final String METHOD_SHOW = "0";
	public static final String METHOD_DELE = "1";
	public static final String METHOD_FINSH = "2";
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String method = req.getParameter("method");
		if (method != null) {
			switch (method) {
			case ListBillServlet.METHOD_SHOW:
				showdept(req, resp);
				break;
				case ListBillServlet.METHOD_DELE:
				delete(req, resp);
				break;
//			case ListBillServlet.METHOD_SUM:
//				sum(req, resp);
//				break;
			case ListBillServlet.METHOD_FINSH:
				finsh(req, resp);
				break;
				
			}

		}
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		
		try {
			IBillService service = ServiceDAOFactory.getBillDAO();
			service.detele(Integer.valueOf(id));
			req.getRequestDispatcher("WEB-INF/view/cashier/addbill.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		};
	}

	private void finsh(HttpServletRequest req, HttpServletResponse resp) {
		Bill bill = new Bill();
		
		try {
			
			String totalPrice = req.getParameter("total");
			String id = req.getParameter("id");
			System.out.println(totalPrice);
			bill.setId(Integer.valueOf(id));
			bill.setBillSum(Double.valueOf(totalPrice));
			bill.setBillPay("否");
			
			IBillService service = ServiceDAOFactory.getBillDAO();
			service.updateprice(bill);;

			req.setAttribute("AddBill_success", "添加成功");
			req.getRequestDispatcher("WEB-INF/view/cashier/addbill.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				req.setAttribute("AddBill_err", "添加失败");
				req.getRequestDispatcher("WEB-INF/view/cashier/addbill.jsp").forward(req, resp);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		
	}

//	private void sum(HttpServletRequest req, HttpServletResponse resp) {
//		String price[] = req.getParameterValues("num");
//		
//		if (price!=null)      
//			{        
//			  for (int i=0;i<price.length;i++)      
//			  {      
//				  double num=Integer.valueOf(price[i]);
//			    totalPrice =totalPrice+num;
//			  }     
//			}
//			String json = String.valueOf(totalPrice);
//			totalPrice=0;
//			PrintWriter out;
//			try {
//				out = resp.getWriter();
//				out.print(json);
//				out.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			
//
//		
//	}

	private void showdept(HttpServletRequest req, HttpServletResponse resp) {
		try {
			List<Dept> deptlist = ServiceDAOFactory.getDeptDAO().findByAll();
			req.getSession().setAttribute("deptlist", deptlist);
			
			req.getRequestDispatcher("WEB-INF/view/cashier/bill.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
